<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Auth;
use App\Userdetail;
use App\Comment_data;

class Commcontroller extends Controller {
    public function comm(){
		$comm = Comment_data::where('reply' , '=' , 0)->get();
        return view("Home\comment" , ['comm'=>$comm]);
    }
   	public function postcomm(request $req){
   		if (!Auth::check()) {
   			return false; 
   		}
   		$noreply = $req->input('noreply');
   		$reply = $req->input('reply');
   		$html = $req->input('html');
   		$row1 = Userdetail::where('uid' , '=' , $req->user()->uid)->first();
   		if (!$row1) {
	   		$imghg = "";
		   	$img = url("images/1.jpg");
	   		$nick = "老鸡奇遇记";
   		} else {
   			$imghg = isset($row1->imghg) ? $row1->imghg : "";
		   	$img = isset($row1->img) ? $row1->img : url("images/1.jpg");
	   		$nick = isset($row1->nick) ? $row1->nick : "老鸡奇遇记";
   		}
   		
   		if (empty($reply)) {
   			if (!empty($noreply)) {
   				$noreply['content'] = htmlspecialchars($noreply['content']);
	   			$data = [
	   						'ip' => $_SERVER["REMOTE_ADDR"],
	   						'uid' => $req->user()->uid,
		   					'name' => $req->user()->name,
		   					'nick' => $nick,
		   					'creat_at' => date('Y-m-d H:i:s' , time()),
		   					'content' => $noreply['content'],
		   					'title' => mb_substr($noreply['content'] , 0 , 4)."...",
		   					'imghg' => $imghg,
		   					'img' => $img,
	   					];
	   			$id = Comment_data::create($data)->commentid;
	   			$data['commentid'] = $id;

	   			$div = '<li class="media" id='.$id.'>';
				$div .= '<div class="media-left"><a href="#">';
				if ($imghg) {
					$div .= '<img class="commhgimg media-object" src="'.$imghg.'" alt="">';
				};
				$div .= '<img class="img-circle media-object" src="'.$img.'" alt=""></a>';
				$div .= '<a class="btn btn-default btn-sm" style="border:0" href="#" data-toggle="tooltip" data-placement="bottom" title="赞本条">赞<span class="badge">0</span></a>';
				$div .= '</div>';
				$div .= '<div class="media-body">';
				$div .= '<div class="media-heading">';
				$div .= '<a class="label label-default" href="">'.$nick.'</a><small class="commtime">'.$data['creat_at'].'</small>';
				$div .= '<a class="btn btn-primary btn-sm commhf" comm-parent='.$id.' comm-id="parent" comm-name="'.$nick.'" href="javascript:" data-whatever="@mdo" data-target="#exampleModal" data-toggle="modal">回复</a>';
				$div .= '</div>';
				$div .= '<p class="commcontent small">';
				$div .=  $data['content'];
				$div .= '</p>';
				$div .= '</div>';
				$div .= '<div class="clear"></div>';
				$div .= '</li>';
				
				Comment_data::updateOrCreate(['commentid'=>$id] , ['main_content'=>$div]);

	   			return json_encode($data);
   			}
   		} else {
   			if (!empty($noreply)) {
   				$row = Comment_data::where('commentid' , '=' , $noreply['commentid'])->first();
   				$reply['content'] = htmlspecialchars($reply['content']);
   				$data = [ 
   							'content' => $reply['content'],
		   					'title' => mb_substr($reply['content'] , 0 , 4)."...",
   							'ip' => $_SERVER["REMOTE_ADDR"],
	   						'uid' => $req->user()->uid,
		   					'name' => $req->user()->name,
		   					'creat_at' => date('Y-m-d H:i:s' , time()),
		   					'reply' => '1',
   						];
   				Comment_data::create($data);
   				$arr = [
   							'nick' => $nick,
   							'imghg' => $imghg,
		   					'img' => $img,
		   					'content' => $reply['content'],
   						];
   				return json_encode($arr);
   			}
   		}

   		if (!empty($html)) {
   			$res = Comment_data::updateOrCreate(['commentid' => $html['commentid']] , ['main_content' => $html['main_content']]);
   			return $res?1:0;
   		}
   	}

   	protected function _addslashes($arr){
		$arr['content'] = htmlspecialchars($arr['content']);
		$arr['content'] = addslashes($arr['content']);
		return $arr;
   	}
}
